<template>
  <section>
    <!--工具条 - 高级查询框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onsale">批量上架</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="offsale">批量下架</el-button>
        </el-form-item>

      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" height="500" style="width: 100%;">
      <el-table-column type="selection" width="55" fixed>
      </el-table-column>
      <el-table-column type="index" width="60" fixed>
      </el-table-column>
      <el-table-column prop="title" label="名称" fixed width="120" sortable>
      </el-table-column>
      <el-table-column prop="cover" label="封面图" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="30px" height="30px">
        </template>
      </el-table-column>
      <el-table-column prop="saleprice" label="原价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="costprice" label="折扣价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新车" width="120" sortable>
        <template slot-scope="scope">
          <span v-if="scope.isnew == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="reigstertime" label="上牌时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="mileage" label="里程数" width="100" sortable>
      </el-table-column>
      <el-table-column prop="shopname" label="店铺名称" width="100" sortable>
      </el-table-column>
      <el-table-column prop="onsaletime" label="上架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="offsaletime" label="下架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: blue">上架</span>
          <span v-else>下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="costeffective" label="超值" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.costeffective == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="rushsale" label="急售" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.rushsale == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="quasinewcar" label="准新车" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.quasinewcar == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="transitivecountry" label="可提全国" min-width="180" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.transitivecountry == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="seller.realName" label="营销人员" width="100" sortable>
      </el-table-column>
      <el-table-column prop="auditstate" label="审核状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.auditstate == 1">已审核</span>
          <span v-else>未审核</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250"  fixed="right">
        <template scope="scope">
          <el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="primary" size="small" @click="handleResources(scope.$index, scope.row)">资源维护</el-button>
          <el-button type="primary" size="small" @click="handleReport(scope.$index, scope.row)">上传检测报告</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!--工具条 - 批量删除&分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.query.pageNumber"
          :page-sizes="[5, 10, 20, 40]"
          :page-size=this.query.pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=this.pageInfo.total
          style="float: right">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="carFormVisible" :close-on-click-modal="false">
      <el-form :model="carForm" label-width="80px" :rules="saveFormRules" ref="carForm">
        <el-form-item label="标题">
          <el-input v-model="carForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload"
              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="carForm.saleprice" style="width:220px;" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价">
          <el-input v-model="carForm.costprice" style="width:220px;" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否新车">
          <el-radio-group v-model="carForm.isnew">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上牌时间">
<!--          <el-date-picker-->
<!--              value-format="yyyy-MM-dd HH:mm:ss"-->
<!--              v-model="carForm.reigstertime"-->
<!--              type="date"-->
<!--              placeholder="请选择日期">-->
<!--          </el-date-picker>-->
          <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="carForm.reigstertime"
              type="datetime"
              placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="里程数">
          <el-input v-model="carForm.mileage" style="width:220px;" type="number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="超值">
          <el-radio-group v-model="carForm.costeffective">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="急售">
          <el-radio-group v-model="carForm.rushsale">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="准新车">
          <el-radio-group v-model="carForm.quasinewcar">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可迁全国">
          <el-radio-group v-model="carForm.transitivecountry">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆类型">
          <el-cascader v-model="carForm.type"
                       :options="carTypeTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
                       clearable></el-cascader>
        </el-form-item>
        <el-form-item label="基本信息">
<!--           <el-input type="textarea" v-model="carForm.carDetail.info"></el-input>-->
          <quill-editor v-model="carForm.carDetail.info" :options="quillOption">
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="资源维护" :visible.sync="carResourceVisible" :close-on-click-modal="false">
      <el-form :model="carResources" label-width="80px" ref="carResourceForm">
        <el-form-item label="主图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload1"
              :on-remove="imgHandleRemove"
              :on-success="imgHandleSuccess"
              :file-list="imgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="副图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload1"
              :on-remove="viceimgHandleRemove"
              :on-success="viceimgHandleSuccess"
              :file-list="viceimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="更多图片">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload1"
              :on-remove="moreimgHandleRemove"
              :on-success="moreimgHandleSuccess"
              :file-list="moreimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carResourceVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveCarResource" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="上传检测报告" :visible.sync="reportVisible" :close-on-click-modal="false">

      <el-form :model="carReportForm" label-width="80px" ref="carResourceForm">
        <el-form-item label="检测员">
          <el-input v-model="carReportForm.detectorname" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="事故排查">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload1"
              :on-remove="accidentHandleRemove"
              :on-success="accidentHandleSuccess"
              :file-list="accidentFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="核心部件检测">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload1"
              :on-remove="coreComponentimgHandleRemove"
              :on-success="coreComponentimgHandleSuccess"
              :file-list="coreComponentimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="常用功能检测">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/upload1"
              :on-remove="commonFunctionimgHandleRemove"
              :on-success="commonFunctionimgHandleSuccess"
              :file-list="commonFunctionimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
<!--        <el-form-item label="外观内饰检测">-->
<!--          <el-upload-->
<!--              class="upload-demo"-->
<!--              action="http://localhost:8080/fastDfs/upload1"-->
<!--              :on-remove="moreimgHandleRemove"-->
<!--              :on-success="moreimgHandleSuccess"-->
<!--              :file-list="moreimgFileList"-->
<!--              list-type="picture">-->
<!--            <el-button size="small" type="primary">点击上传</el-button>-->
<!--            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
<!--          </el-upload>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="reportVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveCarReport" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
//导入富文本编辑框包和样式
import {quillEditor} from "vue-quill-editor"
import "quill/dist/quill.core.css"
import "quill/dist/quill.snow.css"
import "quill/dist/quill.bubble.css"
import quillConfig from "../../common/js/quill-config.js"
export default {
  components:{
    quillEditor
  },
  data() {
    return {
      commonFunctionimgFileList:[],
      coreComponentimgFileList:[],
      accidentFileList:[],
      carReportForm:{
        carId:null,
        detectorname:'',
        accidentInvestigation:'',
        appearanceInspection:'',
        commonFunctionsInvestigation:'',
        coreComponentsInvestigation:''
      },
      reportVisible:false,
      carResourceVisible:false,
      carResources:{
        carId:null,
        list:[]
      },
      imgFileList:[],
      viceimgFileList:[],
      moreimgFileList:[],
      quillOption:quillConfig,
      coverFileList:[],   // 当需要做图片回显时,给它赋值就可以做回显
      query:{
        pageNumber: 1,
        pageSize: 5,
        keyword: ''
      },
      pageInfo:{
        total:0,
        rows:[]
      },
      listLoading: false,
      sels: [],//列表选中列

      carFormVisible: false,//新增修改界面是否显示
      carFormLoading: false,
      saveFormRules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      },
      addLoading:false,
      //新增修改界面数据
      carForm: {
        id:null,
        title:"",
        cover:"",
        saleprice:0,
        costprice:0,
        isnew:0,
        reigstertime:"",
        mileage:null,
        costeffective:0,
        rushsale:0,
        quasinewcar:0,
        transitivecountry:0,
        type:{
          id:null,
          name:""
        },
        typeId:null,
        carDetail:{
          info:""
        }
      },
      carTypeTree:[]
    }
  },
  methods: {
    commonFunctionimgHandleRemove(file, fileList){
      var filePath = file.response ? file.response.objData : file.url;
      this.$http.delete("/fastDfs?path="+filePath)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              this.imgFileList = fileList;
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    commonFunctionimgHandleSuccess(response, file, fileList){
      this.carReportForm.commonFunctionsInvestigation = response.objData
    },
    coreComponentimgHandleRemove(file, fileList){
      var filePath = file.response ? file.response.objData : file.url;
      this.$http.delete("/fastDfs?path="+filePath)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              this.imgFileList = fileList;
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    coreComponentimgHandleSuccess(response, file, fileList){
      this.carReportForm.coreComponentsInvestigation = response.objData
    },
    accidentHandleRemove(file, fileList){
      var filePath = file.response ? file.response.objData : file.url;
      this.$http.delete("/fastDfs?path="+filePath)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              this.imgFileList = fileList;
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    accidentHandleSuccess(response, file, fileList){
      this.carReportForm.accidentInvestigation = response.objData
    },
    saveCarReport(){
      console.log(this.carReportForm)
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        //this.addLoading = true;
        this.$http.post("/carDetail/report",this.carReportForm)
            .then(result => {
              result = result.data;
              if(result.success){
                // 操作成功:要关闭加载框 刷新当前数据  提示删除成功
                this.addLoading = false;
                // 关闭弹框
                this.reportVisible = false;
                this.$message({message: '保存成功!', type: 'success'});
              }else{
                this.$message({message: result.message, type: 'error'});
              }
            })
      });
    },
    handleReport(index,row){
      this.reportVisible = true;
      this.carReportForm.carId = row.id;
      console.log(this.accidentFileList)
      // if(this.accidentFileList.response.objData != null){
      //
      // }
      // this.carReportForm.accidentInvestigation = this.accidentFileList.response.objData
      // this.carReportForm.coreComponentsInvestigation = this.coreComponentimgFileList.response.objData
      // this.carReportForm.commonFunctionsInvestigation = this.commonFunctionimgFileList.response.objData
      this.getCarReportByCarId(row.id)
    },
    // 根据车辆ID获取车辆资源
    getCarReportByCarId(carId){
      this.$http.get("/carDetail/report/"+carId)
          .then(result => {
            result = result.data;
            console.log(result)
            if(result.success){
              var obj = result.data.carDetail;
              if(obj.accidentInvestigation != null && obj.accidentInvestigation != ""){
                this.carReportForm.detectorname = obj.detectorname
                var imgurl = obj.accidentInvestigation;
                var name = imgurl.substring(imgurl.lastIndexOf("/")+1);
                this.accidentFileList.push({name:name,url:imgurl,response:{objData:imgurl}})
              }
              if(obj.coreComponentsInvestigation != null && obj.coreComponentsInvestigation != ""){
                this.carReportForm.detectorname = obj.detectorname
                var imgurl = obj.coreComponentsInvestigation;
                var name = imgurl.substring(imgurl.lastIndexOf("/")+1);
                this.coreComponentimgFileList.push({name:name,url:imgurl,response:{objData:imgurl}})
              }
              if(obj.commonFunctionsInvestigation != null && obj.commonFunctionsInvestigation != ""){
                this.carReportForm.detectorname = obj.detectorname
                var imgurl = obj.commonFunctionsInvestigation;
                var name = imgurl.substring(imgurl.lastIndexOf("/")+1);
                this.commonFunctionimgFileList.push({name:name,url:imgurl,response:{objData:imgurl}})
              }
            }
          })
    },


    onsale(){
      // 获取当前被选中数据的id数组
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾上架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/onsale',ids).then(res => res.data).then(res=> {
          console.log(res)
          this.listLoading = false;
          if(res.success){
            this.$message({message: res.data.msg,type: 'success'});
          }else{
            this.$message({message: res.message,type: 'error'});
          }
          this.getCars();
        });
      }).catch(() => {

      });
    },
    offsale(){
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾下架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/offsale',ids).then((res) => {
          this.listLoading = false;

          if(res.data.success){
            this.$message({
              message: '下架成功',
              type: 'success'
            });
          }else{
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
          this.getCars();
        });
      }).catch(() => {

      });
    },

    // 保存车辆资源数据到后端的
    saveCarResource(){
      // 处理请求参数
      this.dealCarResourceList();
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        //this.addLoading = true;
        this.$http.put("/carResources/batch",this.carResources)
            .then(result => {
              result = result.data;
              if(result.success){
                // 操作成功:要关闭加载框 刷新当前数据  提示删除成功
                this.addLoading = false;
                // 关闭弹框
                this.carResourceVisible = false;
                this.$message({message: '保存成功!', type: 'success'});
              }else{
                this.$message({message: result.message, type: 'error'});
              }
            })
      });
    },
    // 处理车辆资源list
    dealCarResourceList(){
      // 先将this.carResources.list置空,避免数据重复
      this.carResources.list = [];
      debugger
      // 循环主图的文件数组对象,封装往后端传递的数组对象
      if(this.imgFileList && this.imgFileList.length > 0){
        for (let i=0;i<this.imgFileList.length;i++){
          let obj = {resourceUrl:this.imgFileList[i].response.objData,type:1}
          this.carResources.list.push(obj);
        }
      }
      if(this.viceimgFileList && this.viceimgFileList.length > 0){
        for (let i=0;i<this.viceimgFileList.length;i++){
          let obj = {resourceUrl:this.viceimgFileList[i].response.objData,type:2}
          this.carResources.list.push(obj);
        }
      }
      if(this.moreimgFileList && this.moreimgFileList.length > 0){
        for (let i=0;i<this.moreimgFileList.length;i++){
          let obj = {resourceUrl:this.moreimgFileList[i].response.objData,type:3}
          this.carResources.list.push(obj);
        }
      }
    },
    // 根据车辆ID获取车辆资源
    getCarResourcesByCarId(carId){
      this.$http.get("/carResources/carId/"+carId)
          .then(result => {
            result = result.data;
            console.log(result)
            if(result.success){
              for(var i=0;i<result.data.carResourcesList.length;i++){
                var obj = result.data.carResourcesList[i];
                var imgurl = obj.resourceUrl;
                var name = imgurl.substring(imgurl.lastIndexOf("/")+1);
                if(obj.type == 1){
                  this.imgFileList.push({name:name,url:imgurl,response:{objData:imgurl}})
                }
                if(obj.type == 2){
                  this.viceimgFileList.push({name:name,url:imgurl,response:{objData:imgurl}})
                }
                if(obj.type == 3){
                  this.moreimgFileList.push({name:name,url:imgurl,response:{objData:imgurl}})
                }
              }
            }
          })
    },

    imgHandleRemove(file, fileList){
      var filePath = file.response ? file.response.objData : file.url;
      this.$http.delete("/fastDfs?path="+filePath)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              this.imgFileList = fileList;
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    imgHandleSuccess(response, file, fileList){
      this.imgFileList = fileList;
      console.log(this.imgFileList)
    },

    // 副图
    viceimgHandleRemove(file, fileList){
      var filePath = file.response.objData;
      this.$http.delete("/fastDfs?path="+filePath)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              this.viceimgFileList = fileList;
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    viceimgHandleSuccess(response, file, fileList){
      this.viceimgFileList = fileList;
    },

    // 更多图片
    moreimgHandleRemove(file, fileList){
      var filePath = file.response.objData;
      this.$http.delete("/fastDfs?path="+filePath)
          .then(result => {
            result = result.data;
            if(result.success){
              this.$message({message: '删除成功!', type: 'success'});
              this.moreimgFileList = fileList;
            }else{
              this.$message({message: result.message, type: 'error'});
            }
          })
    },
    moreimgHandleSuccess(response, file, fileList){
      this.moreimgFileList = fileList;
    },


    handleResources(index,row){
      this.imgFileList = [];
      this.viceimgFileList = [];
      this.moreimgFileList = [];
      // 展示弹框
      this.carResourceVisible = true;
      this.carResources.carId = row.id;
      // 根据车辆的ID查询该车辆的资源信息,如果有就要回显
      this.getCarResourcesByCarId(row.id);
    },

    // 封面图片删除时调用方法
    coverHandleRemove(file, fileList){
      var filepath = file.response.data.path;
      this.$http.delete("/fastDfs?path="+filepath)
          .then(res => res.data).then(res => {
            console.log(res)
            if(res.success){
              this.$message({message: '删除成功!',type: 'success'});
              this.carForm.cover = "";
            }else {
              this.$message({message: '删除文件失败!',type: 'success'});
            }
          })
    },
    // 封面图片上传成功后的回调
    coverHandleSuccess(response,file,filelist){
      console.log(response)
      this.carForm.cover = response.data.path;
    },
    search(){
      this.query.currentPage = 1;
      this.getCars();
    },
    handleSizeChange(val) {
      this.query.pageSize = val;
      this.query.pageNumber = 1;
      this.getCars();
    },
    handleCurrentChange(val) {
      this.query.pageNumber = val;
      this.getCars();
    },
    //获取部门列表
    getCars() {
      this.listLoading = true;  // 弹出加载框
      this.$http.put("/car",this.query)
          .then(res => res.data).then(res=>{
            console.log(res)
            if(res.success){
              this.pageInfo = res.data.pageList;
              this.listLoading = false;  // 关闭加载框
            }else{
              this.$message({message: '查询分页失败!'+res.message, type: 'error'});
            }
          })
          .catch(result => {
            this.$message({message: "查询分页失败!", type: 'error'});
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/car/"+row.id)
            .then(result => {
              result = result.data;
              if(result.success){
                this.$message({message: '删除成功', type: 'success'});
                // 刷新列表
                this.query.currentPage = 1;
                this.getCars();
                this.listLoading = false;  // 关闭弹出框
              }else{
                this.$message({message: '删除失败!'+result.message, type: 'error'});
              }
            })
      }).catch(() => {
        this.$message({message: '已取消删除', type: 'info'});
      });
    },
    getCarTypeTree(){
      this.$http.get("/carType/tree")
          .then(res => res.data).then(res=>{
            console.log(res)
            if(res.success){
              this.carTypeTree = res.data.carTypes;;
            }else{
              this.$message({message: '获取部门树失败!'+result.message, type: 'error'});
            }
          }).catch(result => {
        this.$message({message: '获取部门树失败!', type: 'error'});
      })
    },
    getEmployeeList(){
      this.$http.get("/employee")
          .then(result => {
            result = result.data;
            if(result.success){
              this.employees = result.result;
            }else{
              this.$message({message: '获取员工列表失败!'+result.message, type: 'error'});
            }
          }).catch(result => {
        this.$message({message: '获取员工列表失败!', type: 'error'});
      })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.carFormVisible = true;
      this.carForm = Object.assign({}, row);
      this.getCarTypeTree()

      if(!this.carForm.carDetail){
        this.carForm.carDetail={
          info:""
        }
      }

      // if(this.carForm.manager == null){
      //   this.carForm.manager = {id:null}
      // }
      // if(row.path != null){
      //   let arrays = row.path.split("/");    // ['', '1', '2', '20'] 根据/分割之后,第一个位置是一个空字符串,是不需要的,另外最后一个值是当前部门的id,而不是父部门的id,所以也不需要
      //   let ids = [];
      //   for(let i=1;i<arrays.length-1;i++){
      //     ids.push(parseInt(arrays[i]))    // 数组分割之后里面的值都是字符串,而部门树的id都是数字,他们匹配不上就不能回显,因此需要parseInt()转一下
      //   }
      //   this.carForm.parent = ids;
      // }
      // //this.carForm.parent = [1,2];
      // // 获取所有的员工数据
      // this.getEmployeeList();
      // // 获取部门树
      // this.getCarTypeTree();
    },
    //显示新增界面
    handleAdd: function () {
      this.carFormVisible = true;
      // 给this.carForm赋初始值,防止第二次点击时将上一次填写的内容带过来
      this.carForm = {
        id:null,
        title:"",
        cover:"",
        saleprice:0,
        costprice:0,
        isnew:0,
        reigstertime:"",
        mileage:null,
        costeffective:0,
        rushsale:0,
        quasinewcar:0,
        transitivecountry:0,
        type:{
          id:null,
          name:""
        },
        typeId:null,
        carDetail:{
          info:""
        }
      }
      // 获取车辆类型树,渲染车辆类型的级联下拉框
      this.getCarTypeTree();
    },
    //新增修改保存方法
    save: function () {
      this.$refs.carForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.carFormLoading = true;
            let para = Object.assign({}, this.carForm);
            // 车辆类型树使用级联下拉框,所获得的值是个数组
            let type = para.type;
            if(type){
              // type目前获取的是个数组,而后端的type是个对象,因此需要转换一下才可以
              para.type = {id:type[type.length-1]};
            }
            this.$http.post("/car",para)
                .then(res => res.data).then(res=>{
                  if(res.success){
                    this.$message({message: '保存成功', type: 'success'});
                    this.carFormLoading = false;
                    this.query.currentPage = 1;
                    this.getCars();
                    this.carFormVisible = false;
                    //this.$refs['carForm'].resetFields();
                    this.coverFileList = []
                  }else{
                    this.$message({message: '保存失败!'+res.message, type: 'error'});
                  }
                }).catch(result => {
              this.$message({message: '保存失败!', type: 'error'});
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        batchRemoveUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getCars();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getCars();  // 做分页查询
  }
}

</script>

<style scoped>

</style>